#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Report;
using TVC.Modules.Rep;
using QueryBuilder;
using FlexCel.Core;
using TVC.Layout;

#endregion

namespace TVC.Modules.Business
{
    public partial class InvoiceTransmit : Form
    {
        string type;
        readonly string _strType = "Invoice";
        public InvoiceTransmit(string type)
        {
            InitializeComponent();
            this.type = type;
            loadComboBox();
            init();
        }
        public MainForm MainForm { get; set; }
        public InvoiceTransmit(DataTable dt, MainForm form, string type)
        {
            InitializeComponent();
            if (dt != null)
            {
                DataColumn dclSelect = new DataColumn("Select", typeof(bool));
                dclSelect.DefaultValue = false;
                if (!dt.Columns.Contains("Select"))
                    dt.Columns.Add(dclSelect);

                TVC.Layout.GridLayout layout = null;
                layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, form.PODInfo.USER_ID), _strType);
                layout.SetUpDGVWithCheck(dgvList);

                dgvList.DataSource = dt;
                dt = null;
            }
            MainForm = form;
            this.type = type;
            loadComboBox();
            cboTransmitRef.Text = "";
            cboReturnRef.Text = "";
            init();
        }
        public object DataSource { get { return dgvList.DataSource; } set { dgvList.DataSource = value; } }

        public void init()
        {
            groupReturn.Visible = type == "R";
            groupTransmit.Visible = type == "T";
        }
        public void loadComboBox()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Name");
            dt.Columns.Add("Value");

            DataRow row;
            row = dt.NewRow();
            row["Name"] = "Sent";
            row["Value"] = "S";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Name"] = "Returned";
            row["Value"] = "TR";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Name"] = "Resent";
            row["Value"] = "RS";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Name"] = "Complete";
            row["Value"] = "C";
            dt.Rows.Add(row);


            cboTransmitStatus.DataSource = dt;
            cboTransmitStatus.DisplayMember = "Name";
            cboTransmitStatus.ValueMember = "Value";

            cboReturnRef.DisplayMember = cboTransmitRef.DisplayMember = "Code";
            cboReturnRef.ValueMember = cboTransmitRef.ValueMember = "Code";
            string sErr = "";
            BUS.InvoiceRegisterControl ctr = new BUS.InvoiceRegisterControl();
            if (type == "T") cboTransmitRef.DataSource = ctr.getTransmitRef(MainForm.PODInfo.Business_ID, ref sErr);
            else cboReturnRef.DataSource = ctr.getReturnRef(MainForm.PODInfo.Business_ID, ref sErr);
        }

        private void btReturnMarkAll_Click(object sender, EventArgs e)
        {
            MarkAll();
        }

        private void MarkAll()
        {
            bool mark = (btReturnMarkAll.Text == "Mark All" || btTransmitMarkAll.Text == "Mark All");

            foreach (DataGridViewRow r in dgvList.Rows)
            {
                r.Cells["colSelect"].Value = mark;
            }
            if (mark)
            {
                btTransmitMarkAll.Text = btReturnMarkAll.Text = "Unmark All";
            }
            else
            {
                btTransmitMarkAll.Text = btReturnMarkAll.Text = "Mark All";
            }
        }

        private void btReturnUpdate_Click(object sender, EventArgs e)
        {
            string sErr = "";
            BUS.InvoiceRegisterControl ctr = new BUS.InvoiceRegisterControl();
            if (cboReturnRef.Text == null || cboReturnRef.Text == "") cboReturnRef.Text = Convert.ToString(ctr.getMaxReturnRef(MainForm.PODInfo.Business_ID, txtReturnDate.Value.Year, txtReturnDate.Value.Month, ref sErr) + 1);

            ctr.BeginTransaction(ref sErr);
            if (sErr == "")
            {
                foreach (DataGridViewRow r in dgvList.Rows)
                {
                    if (sErr != "") break;
                    if (r.Cells["colSelect"].Value != null && (bool)r.Cells["colSelect"].Value == true)
                    {
                        DTO.InvoiceRegisterInfo inf = new DTO.InvoiceRegisterInfo((DataRowView)r.DataBoundItem);
                        inf.ReturnRef = cboReturnRef.Text;
                        inf.ReturnedDate = txtReturnDate.Value;

                        sErr = ctr.Update_Return(MainForm.PODInfo.Business_ID, inf);
                    }

                }
            }

            if (sErr != "")
            {


                ctr.RollbackTransaction(ref sErr);
                MessageBox.Show(sErr, "Success", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
            }

            if (sErr == "")
            {

                ctr.CommitTransaction(ref sErr);
                MessageBox.Show("Your records are successfull updated!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
                DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;
                Close();
                //string args = string.Format("id=ReturnNode_{0}&ReturnRef={1}", MainForm.PODInfo.Business_ID, cboReturnRef.Text);
                Preview("ReturnNote_" + MainForm.PODInfo.Business_ID, "ReturnRef", cboReturnRef.Text);



            }

        }
        private void Preview(string qdid, string name, string val)
        {
            SQLBuilder _sql = SQLBuilder.LoadSQLBuilderFromDataBase(qdid, MainForm.DB, "");
            foreach (Filter f in _sql.Filters)
            {
                if (f.Description == name) { f.ValueFrom = f.FilterFrom = val; }
            }

            ReportGenerator _report = new ReportGenerator(_sql, qdid, "", MainForm.AppConnectString, MainForm.ServerPath + "ReportTemplate\\", MainForm.ServerPath + "Report\\", "");
            _report.UserID = MainForm.PODInfo.EMP_ID;
            ExcelFile exfile = _report.CreateReport();
            frmViewReport frmVi = new frmViewReport(_report.QdCode, MainForm);
            frmVi.filename = _report.QdCode;
            frmVi.Xls = exfile;
            frmVi.BringToFront();
            frmVi.Show();
        }
        private void btClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btTransmitClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btTransmitMarkAll_Click(object sender, EventArgs e)
        {
            MarkAll();
        }

        private void btTransmitUpdate_Click(object sender, EventArgs e)
        {
            string sErr = "";
            BUS.InvoiceRegisterControl ctr = new BUS.InvoiceRegisterControl();
            if (cboTransmitRef.Text == null || cboTransmitRef.Text == "") cboTransmitRef.Text = Convert.ToString(ctr.getMaxTransmitRef(MainForm.PODInfo.Business_ID, txtTransmitDate.Value.Year, txtTransmitDate.Value.Month, ref sErr) + 1);

            ctr.BeginTransaction(ref sErr);
            if (sErr == "")
            {
                foreach (DataGridViewRow r in dgvList.Rows)
                {
                    if (sErr != "") break;
                    if (r.Cells["colSelect"].Value != null && (bool)r.Cells["colSelect"].Value == true)
                    {
                        DTO.InvoiceRegisterInfo inf = new DTO.InvoiceRegisterInfo((DataRowView)r.DataBoundItem);
                        inf.TransMitDate = txtTransmitDate.Value;
                        inf.DeliveryDate = txtTransmitDate.Value;
                        inf.TransMitRef = cboTransmitRef.Text;
                        inf.TransMitStat = cboTransmitStatus.Text;
                        inf.Receiver = txtRecv.Text;

                        sErr = ctr.Update_Transmit(MainForm.PODInfo.Business_ID, inf);
                    }

                }
            }

            if (sErr != "")
            {
                ctr.RollbackTransaction(ref sErr);
                MessageBox.Show(sErr, "Success", MessageBoxButtons.OK, MessageBoxIcon.Error, false);

            }

            if (sErr == "")
            {
                ctr.CommitTransaction(ref sErr);
                MessageBox.Show("Your record is successfull updated!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
                DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK;

                Close();
                Preview("DeliveryNote_" + MainForm.PODInfo.Business_ID, "TransMitRef", cboTransmitRef.Text);
                //string args = string.Format("id=DeliveryNote_{0}&TransMitRef={1}", MainForm.PODInfo.Business_ID, cboTransmitRef.Text);
                //MainForm.SelectCategory("QRS", args);
            }
        }
    }
}